package cn.wizzer.app.web.modules.controllers.platform.user.chat.quickReply;

import cn.wizzer.app.user.modules.models.QuickReply;
import cn.wizzer.app.user.modules.services.QuickReplyService;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 快捷回复用语
 *
 * @author .
 */
@IocBean
@At("/platform/user/quickReply")
public class QuickReplyController {
    @Inject
    @Reference
    private QuickReplyService quickReplyService;


    @At("/")
    @Ok("beetl:/platform/user/chat/quickReply/index.html")
    @RequiresPermissions("user.quickReply")
    public void index() {
    }


    /**
     * 查询（分页）
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.quickReply")
    public Object data(@Param("keyWord") String keyWord,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(quickReplyService.listPage(keyWord, pageNumber, pageSize));
    }


    /**
     * 添加快捷语
     *
     * @param quickReply quickReply
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("user.quickReply.add")
    public Object add(@Param("..") QuickReply quickReply) {
        quickReplyService.insert(quickReply);
        return Result.success();
    }


    /**
     * 查询单条
     *
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json")
    @RequiresPermissions("user.quickReply.edit")
    public Object edit(@Param("id") String id) {
        return Result.success(quickReplyService.fetch(id));
    }


    /**
     * 修改
     *
     * @param quickReply quickReply
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("user.quickReply.edit")
    public Object editDo(@Param("..") QuickReply quickReply) {
        quickReplyService.update(quickReply);
        return Result.success();
    }


    /**
     * 启用
     *
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("user.quickReply.edit")
    public Object enable(@Param("id") String id) {
        quickReplyService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 禁用
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("user.quickReply.edit")
    public Object disable(@Param("id") String id) {
        quickReplyService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }


    /**
     * 删除
     *
     * @param id String
     * @return Object
     */
    @At({"/delete/?", "/delete"})
    @Ok("json")
    @RequiresPermissions("user.quickReply.delete")
    public Object delete(String id, @Param("ids") String[] ids) {
        if (null != ids && ids.length > 0) {
            quickReplyService.delete(ids);
        } else {
            quickReplyService.delete(id);
        }
        return Result.success();
    }

}
